package com.lz.ccjdemo.utils.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

public class SimpleDialog {
    AlertDialog alertDialog;

    public SimpleDialog(Builder builder) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(builder.getContext());
        alertDialogBuilder.setTitle(builder.getTitleStr());
        alertDialogBuilder.setMessage(builder.getContextStr());
        if (!TextUtils.isEmpty(builder.getConfirmStr())) {
            alertDialogBuilder.setPositiveButton(builder.getConfirmStr(), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // 点击确认按钮的逻辑
                    if (builder.dialogListener != null) {
                        builder.dialogListener.onConfirm(alertDialog);
                    } else {
                        alertDialog.dismiss();
                    }
                }
            });
        }
        if (!TextUtils.isEmpty(builder.getCancelStr())) {
            alertDialogBuilder.setNegativeButton(builder.getCancelStr(), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // 点击取消按钮的逻辑
                    if (builder.dialogListener != null) {
                        builder.dialogListener.onCancel(alertDialog);
                    } else {
                        alertDialog.dismiss();
                    }
                }
            });
        }
        alertDialog = alertDialogBuilder.create();
        alertDialog.setCancelable(builder.isCancel());
        alertDialog.show();
        // 隐藏 Cancel 按钮

        if (TextUtils.isEmpty(builder.getCancelStr())) {
            //如果没有传入文案 认定不显示取消按钮
            Button cancelBtn = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            cancelBtn.setVisibility(View.GONE);
        }
    }

    public static class Builder {

        private Context context;
        private String confirmStr = "确定";
        private String cancelStr = "取消";
        private String titleStr;
        private String contextStr;
        private boolean isCancel = true;

        private OnDefaultDialogListener dialogListener;


        public Builder(Context context) {
            this.context = context;
        }

        public Context getContext() {
            return context;
        }

        public String getConfirmStr() {
            return confirmStr;
        }

        public Builder setConfirmStr(String confirmStr) {
            this.confirmStr = confirmStr;
            return this;
        }

        public String getCancelStr() {
            return cancelStr;
        }

        public Builder setCancelStr(String cancelStr) {
            this.cancelStr = cancelStr;
            return this;
        }

        public String getTitleStr() {
            return titleStr;
        }

        public Builder setTitleStr(String titleStr) {
            this.titleStr = titleStr;
            return this;
        }

        public String getContextStr() {
            return contextStr;
        }

        public Builder setContextStr(String contextStr) {
            this.contextStr = contextStr;
            return this;
        }

        public boolean isCancel() {
            return isCancel;
        }

        public Builder setCancel(boolean cancel) {
            isCancel = cancel;
            return this;
        }

        public OnDefaultDialogListener getDialogListener() {
            return dialogListener;
        }

        public Builder setDialogListener(OnDefaultDialogListener dialogListener) {
            this.dialogListener = dialogListener;
            return this;
        }

        public SimpleDialog build() {
            return new SimpleDialog(this);
        }
    }

    public interface OnDefaultDialogListener {
        void onCancel(AlertDialog dialog);

        void onConfirm(AlertDialog dialog);
    }

}
